<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button @click="addVisible=true" type="primary" size="mini">添加分类</el-button>
      </div>
      <!-- table上有 border显示边框  stripe斑马底纹
           table-column 上有 type="index" 可显示从1开始的索引值-->
        <el-table
          border
          stripe
          :data="cateList"
          style="width: 100%">
          <el-table-column
            type="index"
            label="序号"
            width="48">
            <!-- <template v-slot="scope">
                {{scope.$index+1}}
            </template> -->
          </el-table-column>
          <el-table-column
            prop="cate_name"
            label="分类名称"
           >
          </el-table-column>
          <el-table-column
            prop="cate_alias"
            label="分类别名">
          </el-table-column>
            <el-table-column
            label="操作">
           <template v-slot="scope">
                <el-button @click="showEdit(scope.row,scope.$index)" type="primary" size="mini">修改</el-button>
            <el-button @click="del(scope.row.id,scope.$index)" type="danger" size="mini">删除</el-button>
           </template>
          </el-table-column>
        </el-table>
    </el-card>
    <el-dialog
        @closed ="$refs.addForm.resetFields()"
        title="添加文章分类"
        :visible.sync="addVisible"
        width="35%"
        >
        <el-form :model="addForm" :rules="addRules" ref="addForm" label-width="70px" class="demo-addForm">
            <el-form-item label="分类名称" prop="cate_name">
                <el-input v-model="addForm.cate_name" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item label="分类别名"  prop="cate_alias">
                <el-input v-model="addForm.cate_alias" maxlength="15"> </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="addVisible = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="addCate">确 定</el-button>
            </span>
    </el-dialog>
      <el-dialog
        @closed ="$refs.editForm.resetFields()"
        title="修改分类"
        :visible.sync="editVisible"
        width="35%"
        >
        <el-form :model="editForm" :rules="addRules" ref="editForm" label-width="70px" class="demo-editForm">
            <el-form-item label="分类名称" prop="cate_name">
                <el-input v-model="editForm.cate_name" maxlength="10"></el-input>
            </el-form-item>
            <el-form-item label="分类别名"  prop="cate_alias">
                <el-input v-model="editForm.cate_alias" maxlength="15"> </el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="mini" @click="editVisible = false">取 消</el-button>
            <el-button size="mini" type="primary" @click="editCate">确 定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data() {
    return {
      cateList: [],
      addVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addRules: {
        cate_name: [
          { required: 1, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名必须是1-10位的非空字符', trigger: ['blur', 'change'] }
        ],
        cate_alias: [
          { required: 1, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名必须是1-15位的字母数字', trigger: ['blur', 'change'] }
        ]
      },
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      editVisible: false
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('/my/cate/list')
      //   console.log(res)
      if (res.code !== 0) return this.$message.error(res.message)
      this.cateList = res.data
    },
    addCate() {
      // 1.兜底校验 (添加分类=>确定)
      this.$refs.addForm.validate(async valid => {
        // 2. 发送请求
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 3. 成功后关闭对话框
        this.addVisible = false
        // 4. 重新请求 最新文章分类
        this.getCateList()
      })
    },
    showEdit(row, index) {
    //   console.log(row)
    //   console.log(index)
      if (index === 0 || index === 1) {
        return this.$message.warning('第一条和第二条数据不可以修改')
      }
      // scope不能使用插值表达式渲染 , $route也不行, 若想查看它们的内部成员,可以使用打印台
      // 1.点击修改,弹出 修改对话框
      this.editVisible = true
      // 2.渲染, 使用作用域插槽, 展开当前行的数据并赋值 到对话框中
      this.editForm = { ...row }
    },
    editCate() {
      // 1. 兜底校验(修改分类=>确定)
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        // 2. 发送请求
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        // console.log(res)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        // 3. 成功后,关闭对话框
        this.editVisible = false
        // 4. 重新请求 最新分类
        this.getCateList()
      })
    },
    del(id, index) {
      if (index === 0 || index === 1) {
        return this.$message.warning('第一条和第二条数据不可以删除')
      }
      this.$confirm('此操作将永久删除该分类,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const { data: res } = await this.$http.delete('/my/cate/del', { params: { id } })
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.getCateList()
      })
        .catch(e => e)
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
